class ProfileController {
    
    def imageService

    static roleAccess =  [
            [action:["*"], roles:["*"]],
        ]

    def index = {
        if (params["_action_Save"]) {
            save()
        }
        else if (params["_action_Update"]) {
            update()
        }
        else if (params["_action_Delete"]) {
            delete()
        } else {
                redirect(action:show,params:params)
        }
    }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [ save:'POST', update:'POST']

    def show = {

        def tempUser

        println "id=" + params.id
        if(params.id=='' || params.id==null || params.id==session?.user?.name) {
          tempUser = session.user
        } else {
          tempUser = Guser.findByName(params.id)
        }
        if(tempUser?.profile) {
          def gprofile = tempUser.profile
        }


        if(tempUser?.profile==null) {
            flash.message = "Gprofile not found with id ${params.id}"
            redirect(controller:"default")
        } else {
            return [ gprofile : tempUser.profile ]
        }
    }

    def edit = {
        //def gprofile = Gprofile.get( params.id )
        def gprofile = session.user?.profile

        if(!gprofile) {
            flash.message = "Gprofile not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ gprofile : gprofile ]
        }
    }


    def update = {

        def gprofile = session.user?.profile

        
        if(gprofile) {

            def username = session.user?.name
            def file = params.imageFile
            imageService.thumbnail(file, username+'.png', grailsApplication)
            params.imageName = username

            gprofile.properties = params
          
            params.findAll{pm->
                    pm.key.length()>2 && pm.key[0..2]=="otm"
            }.each {  p->
                    def keys = p.key.split("_");
                    def classkey = keys[1]
                    def addtokeyorig = keys[2]
                    def addtokey = 'addTo' + addtokeyorig[0].toUpperCase() + addtokeyorig[1..-1]
                    classkey = classkey[0].toUpperCase() + classkey[1..-1]

                    gprofile."$addtokeyorig"= null

                    p.value.each { val->
                            def temp = Class.forName(classkey, true, grailsApplication.classLoader).newInstance()
                            temp = temp.get(val)
                            gprofile."$addtokey"(temp)
                    }
            }


            if(!gprofile.hasErrors() && gprofile.save()) {
                session?.user?.profile = gprofile
                flash.message = "Your profile was updated!"
                redirect(action:show)
            }
            else {
                render(view:'edit',model:[gprofile:gprofile])
            }
        }
        else {
            flash.message = "Profile id ${params.id} not found"
            redirect(action:edit,id:params.id)
        }
    }

}
